/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-11-07 13:55:52
*/
package com.ueboot.weishop.api.controller.lottery;

import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.SessionUtils;
import com.ueboot.weishop.api.controller.lottery.vo.LotterySubmitReq;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.lottery.LotteryRecordEntity;
import com.ueboot.weishop.entity.order.OrderEntity;
import com.ueboot.weishop.entity.order.OrderEnum;
import com.ueboot.weishop.service.cache.CacheService;
import com.ueboot.weishop.service.customer.CustomerService;
import com.ueboot.weishop.service.lottery.LotteryInfoService;
import com.ueboot.weishop.service.lottery.LotteryRecordService;
import com.ueboot.weishop.service.order.OrderService;
import jodd.datetime.JDateTime;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by yangkui  on 2017-11-07 13:55:52.
 * 用户下单后进行抽奖操作
 *
 * @author yangkui
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/private/lotteryInfo")
public class LotteryInfoController {

	@Autowired
	private LotteryInfoService lotteryInfoService;


	@Autowired
	private OrderService orderService;

	@Autowired
	private CacheService cacheService;

	@Autowired
	private CustomerService customerService;

	@Autowired
	private LotteryRecordService lotteryRecordService;
	/**
	 * SSP抽奖 ﻿SSP可中，50 100 200 中奖比例为 85 10 5
	 */
	private static final String LOTTY_TYPE_100 = "100";
	/**
	 * 福宝零件抽奖
	 */
	private static final String LOTTY_TYPE_101 = "101";
	static int[] sspLottery = new int[100];

	//初始化中奖概率
	static {
		for (int j = 0; j < 100; j++) {
			if (j < 85) {
				//50块
				sspLottery[j] = 50;
			} else if (j < 95) {
				//100块
				sspLottery[j] = 100;
			} else {
				//200块
				sspLottery[j] = 200;
			}
		}

	}

	/**
	 * 执行抽奖逻辑
	 *
	 * @param req
	 * @retur
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Response<Integer> save(@RequestBody LotterySubmitReq req) {
		//判断时间是否超过2017-11-20 0点
		JDateTime dateTime = new JDateTime(2017,11,20,0,0,0,0);
		JDateTime now = new JDateTime();
		if(!now.isBefore(dateTime)){
		    log.info("已经超过20号，不再抽奖！");
            return new Response<>(0);
        }

		//查询订单是否已经抽奖过
		Long count = cacheService.countOrderLottery(req.getOrderNo());
		if (count != null && count > 0) {
			throw new BusinessException("100", "当前订单已经进行过抽奖了，无法再次抽奖!");
		}
		//查询订单对应的活动
		OrderEntity order = orderService.findByOrderNo(req.getOrderNo());
		Assert.notNull(order, "订单不存在！");
		if (!OrderEnum.ORDER_STATUS_1.equals(order.getOrderStatus()) && !OrderEnum.ORDER_STATUS_2.equals(order.getOrderStatus())) {
			throw new BusinessException("102", "当前订单尚未支付成功，无法参加抽奖!");
		}
		String categoryCode = order.getCategoryCode();
		int lotteryValue = 0;
		//TODO 临时写死，针对不同的分类订单进行抽奖活动
		if (LOTTY_TYPE_100.equals(categoryCode)) {
			int r = RandomUtils.nextInt(100);
			lotteryValue = sspLottery[r];
		} else if (LOTTY_TYPE_101.equals(categoryCode)) {
			lotteryValue = 50;
		} else {
			throw new BusinessException("101", "当前商品不参加抽奖活动!");
		}
		log.info("订单号:{},抽中:{}元代金券", req.getOrderNo(), lotteryValue);
		//生成抽奖记录
		LotteryRecordEntity record = createLotteryOrder(lotteryValue, order);
		lotteryRecordService.save(record);
		cacheService.incrOrderLotteryInfo(req.getOrderNo());
		return new Response<>(lotteryValue);
	}

	/**
	 * 创建抽奖订单
	 *
	 * @param value
	 * @param order
	 * @return
	 */
	private LotteryRecordEntity createLotteryOrder(int value, OrderEntity order) {
		LotteryRecordEntity lotteryRecord = new LotteryRecordEntity();
		lotteryRecord.setWinType(LotteryRecordEntity.WIN_TYPE_0);
		//直减减去的金额
		lotteryRecord.setDiscountMinusPrice(value);
		lotteryRecord.setOrder(order);
		lotteryRecord.setCategoryCode(order.getCategoryCode());
		CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
		lotteryRecord.setCustomer(customerInfoEntity);
		lotteryRecord.setExtStatus(0);
		return lotteryRecord;
	}

}
